package cn.com.pagoda.ideplugin.ui;

import cn.com.pagoda.ideplugin.service.YamlPropertiesConverterService;
import com.intellij.openapi.actionSystem.AnActionEvent;
import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;
import org.fife.ui.rtextarea.RTextScrollPane;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @author <a href="mailto:kindbgen@gmail.com">Kindbgen<a/>
 * @description YAML-Properties转换器
 * @date 2023/6/10
 * @since 1.0
 */
public class YamlPropertiesConverterPanel implements ActionListener {

    private final YamlPropertiesConverterService service;
    private final AnActionEvent anActionEvent;
    private final JPanel panel;
    private final RSyntaxTextArea yamlTextArea;
    private final RSyntaxTextArea propertiesTextArea;
    private final JButton convertToPropertiesButton;
    private final JButton convertToYamlButton;

    public YamlPropertiesConverterPanel(AnActionEvent anActionEvent, JPanel mainPanel) {

        this.anActionEvent = anActionEvent;
        panel = new JPanel(new GridLayout(1, 2));

        yamlTextArea = new RSyntaxTextArea(20, 30);
        propertiesTextArea = new RSyntaxTextArea(20, 30);
        var font = new Font("Microsoft YaHei", Font.PLAIN, 14);
        yamlTextArea.setFont(font);
        propertiesTextArea.setFont(font);

        this.service = new YamlPropertiesConverterService(anActionEvent, panel, yamlTextArea, propertiesTextArea);

        convertToPropertiesButton = new JButton("Convert to Properties");
        convertToYamlButton = new JButton("Convert to YAML");

        convertToPropertiesButton.addActionListener(this);
        convertToYamlButton.addActionListener(this);

        JPanel leftPanel = new JPanel(new BorderLayout());
        leftPanel.add(new JLabel("YAML:"), BorderLayout.NORTH);

        RTextScrollPane yamlScrollPane = new RTextScrollPane(yamlTextArea);
        yamlScrollPane.setLineNumbersEnabled(true);
        yamlScrollPane.setFoldIndicatorEnabled(true);
        leftPanel.add(yamlScrollPane, BorderLayout.CENTER);
        leftPanel.add(convertToPropertiesButton, BorderLayout.SOUTH);

        JPanel rightPanel = new JPanel(new BorderLayout());
        rightPanel.add(new JLabel("Properties:"), BorderLayout.NORTH);

        RTextScrollPane propertiesScrollPane = new RTextScrollPane(propertiesTextArea);
        propertiesScrollPane.setLineNumbersEnabled(true);
        propertiesScrollPane.setFoldIndicatorEnabled(true);
        rightPanel.add(propertiesScrollPane, BorderLayout.CENTER);
        rightPanel.add(convertToYamlButton, BorderLayout.SOUTH);

        panel.add(leftPanel);
        panel.add(rightPanel);
        mainPanel.add(panel, getClass().getSimpleName());

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == convertToPropertiesButton) {
            service.convertToProperties();
        } else if (e.getSource() == convertToYamlButton) {
            service.convertToYaml();
        }
    }

}
